package com.telecom.weixi.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.telecom.weixi.R;

public class GridAdapter extends BaseAdapter {
	private List<Map<String, Integer>> mGridData;
	private Context mContext;

	public static final String TAG_MENU_NAME = "menu_name"; // String
	public static final String TAG_MENU_ICON = "menu_icon"; // drawable
	public static final String TAG_TEXT_COLOR = "text_color"; // int

	private LayoutInflater inflater;
	private ViewHolder holder = null;

	public GridAdapter(Context context) {
		mGridData = new ArrayList<Map<String, Integer>>();
		mContext = context;
		inflater = LayoutInflater.from(mContext);
	}

	public void addItem(Map<String, Integer> item) {
		mGridData.add(item);
	}

	public void clearData() {
		mGridData.clear();
	}

	@Override
	public int getCount() {
		return mGridData.size();
	}

	@Override
	public Object getItem(int position) {

		return mGridData.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	private class ViewHolder {
		ImageView icon_iv;
		TextView name_tv;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.bu_img_tv_item, null);
			holder = new ViewHolder();
			holder.icon_iv = (ImageView) convertView.findViewById(R.id.gridView_ImageViewItem);
			holder.name_tv = (TextView) convertView.findViewById(R.id.gridView_TextViweItem);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		Map<String, Integer> itemData = mGridData.get(position);
		int name = itemData.get(TAG_MENU_NAME);
		int color = itemData.get(TAG_TEXT_COLOR);
		int icon = itemData.get(TAG_MENU_ICON);

		if (color == -1) {
			holder.name_tv.setTextColor(mContext.getResources().getColor(R.color.menu_item_color));
		} else {
			holder.name_tv.setTextColor(mContext.getResources().getColor(color));
		}
		holder.icon_iv.setImageResource(icon);
		holder.name_tv.setText(name);
		return convertView;
	}

}
